package com.caigou.service.impl;




import com.caigou.bean.cms.entity.Video;
import com.caigou.bean.cms.entity.VideoFolder;
import com.caigou.bean.cms.param.*;
import com.caigou.bean.exam.entity.Users;
import com.caigou.component.OSSComponent;
import com.caigou.constant.NewsConstant;
import com.caigou.enums.FailResultEnum;
import com.caigou.enums.SuccessResultEnum;
import com.caigou.enums.VideoEnum;
import com.caigou.mapper.cms.VideoFolderMapper;
import com.caigou.mapper.cms.VideoMapper;
import com.caigou.service.VideoService;
import com.caigou.util.FileUtil;
import com.caigou.util.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.framework.AopContext;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import ws.schild.jave.MultimediaInfo;
import ws.schild.jave.MultimediaObject;

import javax.annotation.Resource;
import java.io.File;
import java.util.Date;
import java.util.List;

@Slf4j
@Service("videoService")
public class VideoServiceImpl implements VideoService {

    @Resource
    private VideoFolderMapper videoFolderMapper;

    @Resource
    private VideoMapper videoMapper;

    @Resource
    private OSSComponent ossComponent;

    /**
     * 获取视频库根文件夹
     * @return
     */
    public VideoFolder getRootVideoFolder(){
        return videoFolderMapper.getRootVideoFolder();
    }

    /**
     * 创建视频库文件夹
     * @param superId
     * @param folderName
     * @param userId
     * @return
     */
    @Transactional
    public Result createVideoFolder(Integer superId, String folderName, String userId){
        //获取父文件夹
        VideoFolder parent = videoFolderMapper.selectByPrimaryKey(superId);
        if(parent==null){
            return Result.error("视频库父文件夹不存在");
        }
        //获取父文件夹信息
        Integer parentLevel = parent.getLevel();
        String parentPath = parent.getPath();
        if(parentLevel<1){
            log.warn("视频父目录级别level错误，leve值应该大于0，实际level={}", parentLevel);
            return Result.error("视频库父文件夹级别错误");
        }
        //保存本级文件夹信息
        VideoFolder videoFolder = new VideoFolder();
        long timestamp = System.currentTimeMillis();
        Date current = new Date(timestamp);
        videoFolder.setCreateTime(current);
        videoFolder.setUpdateTime(current);
        videoFolder.setFolderName(folderName);
        videoFolder.setHasSubfolder(0);
        videoFolder.setCreateUserId(userId);
        videoFolder.setUpdateUserId(userId);
        videoFolder.setLevel(parentLevel+1);
        videoFolder.setSuperId(superId);
        int scount = 0;
        try{
            scount = this.saveVideoFolder(videoFolder);
        }catch(DataAccessException e){
            log.error(e.getMessage(), e);
            return Result.custom(400, "创建文件夹名称重复", "创建文件夹名称重复");
        }

        //更新本级文件夹path
        Integer folderId = videoFolder.getId();
        String path = parentPath+folderId+"/";
        videoFolder.setPath(path);
        videoFolder.setUpdateTime(new Date(System.currentTimeMillis()));
        int ucount = videoFolderMapper.updatePath(videoFolder);
        log.info("保存视频文件夹结果={}， 更新视频文件夹结果={}, id={}，userId={}", scount, ucount, folderId, userId);

        //更新父级文件夹hashSubfolder
        if(parent.getHasSubfolder().intValue()==0){//父级文件夹没有子文件夹
            parent.setHasSubfolder(1);
            parent.setUpdateTime(new Date(System.currentTimeMillis()));
            int count = videoFolderMapper.updateHasSubfolder(parent);
            log.info("更新父级视频文件夹hasSubfolder结果={}，id={}", count, parent.getId());
        }
        return Result.success("用户创建视频库文件夹成功");
    }

    public int saveVideoFolder(VideoFolder videoFolder){
        return videoFolderMapper.insertSelective(videoFolder);
    }

    public List<VideoFolder> listVideoFolderBySuperId(Integer superId){
        return videoFolderMapper.listVideoFolderBySuperId(superId);
    }

    public VideoFolder getVideoFolder(Integer id){
        return videoFolderMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据关键词分页查询视频
     * @param folderId
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageInfo<Video> queryVideoIncludingSubfolders(Integer folderId, String keyword, Integer pageNum, Integer pageSize){
        VideoFolder videoFolder = videoFolderMapper.selectByPrimaryKey(folderId);
        String path = videoFolder.getPath();
        Page<?> page = PageHelper.startPage(pageNum, pageSize);
        List<Video> videoList = videoMapper.queryVideoByPath(path, keyword);
        PageInfo<Video> videoPageInfo = new PageInfo<>(videoList);
        return videoPageInfo;
    }

    public PageInfo<Video> queryVideoIncludingSubfolders(VideoQueryParam videoQueryParam, Users user){
        Integer folderId = videoQueryParam.getFolderId();
        if(folderId!=null){
            VideoFolder videoFolder = videoFolderMapper.selectByPrimaryKey(folderId);
            String path = videoFolder.getPath();
            videoQueryParam.setPath(path);
        }
        Integer videoType = videoQueryParam.getVideoType();
        if(videoType!=null){
            String strVideoType = null;
            if(videoType== VideoEnum.VIDEO_MP4.getType()){
                strVideoType=VideoEnum.VIDEO_MP4.getNote();
            }else if(videoType==VideoEnum.VIDEO_MPG.getType()){
                strVideoType=VideoEnum.VIDEO_MPG.getNote();
            }else if(videoType==VideoEnum.VIDEO_AVI.getType()){
                strVideoType=VideoEnum.VIDEO_AVI.getNote();
            }else if(videoType==VideoEnum.VIDEO_MOV.getType()){
                strVideoType=VideoEnum.VIDEO_MOV.getNote();
            }
            videoQueryParam.setStrVideoType(strVideoType);
        }
        if(videoQueryParam.getOnlyMine()!=null && videoQueryParam.getOnlyMine()==1){//只看自己
            videoQueryParam.setUploadUserId(user.getUserid());
        }
        if(videoQueryParam.getDeleteStatus()==null){
            videoQueryParam.setDeleteStatus(0);
        }
        Long intKeyword = null;
        try{
            intKeyword = Long.parseLong(videoQueryParam.getKeyword());
            videoQueryParam.setIntKeyword(intKeyword);
        }catch(Exception e){
            //log.error(e.getMessage(), e);
        }
        Page<?> page = PageHelper.startPage(videoQueryParam.getPageNum(), videoQueryParam.getPageSize());
        List<Video> videoList = videoMapper.advancedQueryVideoByPath(videoQueryParam);

        //设置视频文件的所在目录名称（全路径）
        buildFullFolderName(videoList);

        PageInfo<Video> videoPageInfo = new PageInfo<>(videoList);
        return videoPageInfo;
    }

    /**
     *
     * @param files
     * @param videoNotes
     * @param folderId
     * @param videoSource
     * @param videoEditor
     * @param userId
     * @return
     */
    public Result uploadVideo(MultipartFile[] files, String[] videoNotes, String[] videoImages, Integer folderId, String videoSource, String videoEditor, String userId){
        log.info("---------service start--------");
        VideoFolder videoFolder = null;
        if(folderId==null){
            videoFolder = videoFolderMapper.getDefaultVideoFolder();
        }else{
            videoFolder = getVideoFolder(folderId);
        }
        if(videoFolder==null){
            return Result.error("音频文件夹id错误，没有这个音频文件夹或者没有默认音频文件夹");
        }
        String path = videoFolder.getPath();
        int index = path.indexOf("/");
        String vFolder = NewsConstant.VIDEO_ROOT+path.substring(index);
        log.info("上传视频文件到oss的目录={}", vFolder);
        //多文件上传
        int failCount = 0;
        int successCount = 0;
        int fileCount = files.length;
        if(fileCount==1 && videoNotes.length==0){
            videoNotes = new String[1];
            videoNotes[0] = "";
        }
        if(fileCount==1 && videoImages.length==0){
            videoImages = new String[1];
            videoImages[0] = "";
        }
        StringBuffer sb = new StringBuffer("第");
        for(int i=0; i<files.length; i++) {
            MultipartFile file = files[i];
            log.info("before 上传视频videoNote={}, i={}", videoNotes[i], i);
            String videoNote = videoNotes[i];
            String videoImage = videoImages[i];//视频占位图
            log.info("上传视频videoNote={}, i={}", videoNote, i);
            //普通上传
            String fileUrl = ossComponent.uploadToOSS(file, vFolder);
            //分片上传
            //String fileUrl = ossComponent.uploadLargeToOSS(file, vFolder);
            if(fileUrl!=null&&fileUrl.length()>0) {
                long timestamp = System.currentTimeMillis();
                Date currentDate = new Date(timestamp);
                Video video = new Video();
                video.setCreateTime(currentDate);
                video.setFolderId(videoFolder.getId());
                video.setCreateUserId(userId);
                video.setUpdateTime(currentDate);
                video.setUpdateUserId(userId);
                video.setVideoNote(videoNote);
                video.setVideoImage(videoImage);
                video.setVideoSource(videoSource);
                video.setVideoEditor(videoEditor);

                //计算视频文件时长
                log.info("第{}个视频，计算视频时长开始==="+System.currentTimeMillis(), i);
                File tempFile = null;
                try{
                    tempFile = FileUtil.multipartFileToFile(file);
                    MultimediaObject instance = new MultimediaObject(tempFile);
                    MultimediaInfo result = instance.getInfo();
                    long ls = result.getDuration()/1000;
                    video.setVideoDuration(new Long(ls).intValue());
                }catch(Exception e){
                    log.error(e.getMessage(), e);
                    video.setVideoDuration(0);
                }finally {
                    if(tempFile!=null) {
                        //清除临时文件
                        FileUtil.deleteTempFile(tempFile);
                    }
                }
                log.info("第{}个视频计算视频时长结束==="+System.currentTimeMillis(), i);

                video.setVideoFormat(file.getContentType());
//                video.setVideoIsUse(0);
                video.setVideoLength(new Long(file.getSize()).intValue()/(1024));
                video.setVideoName(file.getOriginalFilename());
                video.setVideoUrl(fileUrl);

                int count = saveVideo(video);
                if(count==1){
                    successCount++;
                }else{
                    failCount++;
                    sb.append((i+1)).append("、");
                }
            }else{
                failCount++;
            }
        }
        if(failCount==0){//全部成功
            return Result.success("视频上传成功");
        }else if(successCount==0){//全部失败
            return Result.error(failCount+"个视频上传全部失败", failCount+"个视频上传全部失败");
        }else{//部分成功，部分失败
            sb.deleteCharAt(sb.length() - 1);//删除最后一个字符
            return Result.custom(400,failCount+"个（"+sb.toString()+")视频上传失败,"+successCount+"个视频上传成功",failCount+"个（"+sb.toString()+")视频上传失败,"+successCount+"个视频上传成功");
        }
    }

    /**
     * 删除视频库文件夹，实际是逻辑删除
     * @param id 视频库文件夹id
     * @return
     */
    public Result deleteVideoFolder(Integer id){
        VideoFolder videoFolder = videoFolderMapper.selectByPrimaryKey(id);
        //判断是否默认文件夹
        if(videoFolder.getIsDefaultFolder()==1){
            return Result.error(FailResultEnum.VIDEO_FOLDER_ERROR, "视频文件夹为默认文件夹，不能删除");
        }
        //判断是否有子文件夹
        if(videoFolder.getHasSubfolder()==1){
            return Result.error(FailResultEnum.VIDEO_FOLDER_ERROR, "视频文件夹有子文件夹，不能删除");
        }
        //判断是否有视频文件
        int videoCount = videoMapper.getVideoCount(id);
        if(videoCount>0){
            return Result.error(FailResultEnum.VIDEO_FOLDER_ERROR, "视频库文件夹中有视频，不能删除");
        }

//        //逻辑删除，更新delete_status状态
//        videoFolder.setDeleteStatus(NewsConstant.DELETE_STATUS_DELETE);
//        videoFolder.setUpdateTime(new Date(System.currentTimeMillis()));
//        int deleteCount = videoFolderMapper.updateDeleteStatus(videoFolder);
        //物理删除
        int deleteCount = videoFolderMapper.deleteByPrimaryKey(id);
        if(deleteCount>0){
            //判断父文件夹下面还有没有状态正常的子文件夹
            Integer superId = videoFolder.getSuperId();
            int siblingCount = videoFolderMapper.getSubfolderCount(superId);
            if(siblingCount==0){
                VideoFolder parent = new VideoFolder();
                parent.setId(superId);
                parent.setHasSubfolder(0);
                parent.setUpdateTime(new Date(System.currentTimeMillis()));
                int pCount = videoFolderMapper.updateHasSubfolder(parent);
                log.info("更新视频库父文件夹id={}的hasSubfolder状态为0,更新结果为{}", superId, pCount);
            }
            return Result.success(SuccessResultEnum.SUCCESS, "删除视频库文件夹成功");
        }else{
            return Result.success(SuccessResultEnum.SUCCESS, "删除视频库文件夹失败");
        }
    }


    public int saveVideo(Video video){
        return videoMapper.insertSelective(video);
    }

    public Result updateVideo(VideoParam videoParam, Users user){
        Video video = new Video();
        video.setId(videoParam.getId());
        video.setVideoName(videoParam.getVideoName());
        video.setVideoNote(videoParam.getVideoNote());
        video.setVideoImage(videoParam.getVideoImage());
        video.setVideoSource(videoParam.getVideoSource());
        video.setVideoEditor(videoParam.getVideoEditor());
        video.setFolderId(videoParam.getFolderId());
        video.setUpdateTime(new Date(System.currentTimeMillis()));
        video.setUpdateUserId(user.getUserid());

        int count = videoMapper.updateVideo(video);
        if(count>0){
            return Result.success("修改视频属性成功");
        }else{
            return Result.error("修改视频属性失败");
        }
    }

    public Result updateVideoName(Integer id, String newVideoName, String userId){
        Video video = new Video();
        video.setId(id);
        video.setVideoName(newVideoName);
        video.setUpdateTime(new Date(System.currentTimeMillis()));
        video.setUpdateUserId(userId);
        int count = videoMapper.updateVideo(video);
        if(count>0){
            return Result.success("修改视频名称成功");
        }else{
            return Result.error("修改视频名称失败");
        }
    }

    @Transactional
    public Result deleteVideo(Integer id){
        Video video = videoMapper.selectByPrimaryKey(id);
        if(video==null){
            return Result.error("没有该视频");
        }
        //首先删除数据库总该视频数据
        int count = videoMapper.deleteByPrimaryKey(id);
        //其次删除oss上该视频文件
        if(count>0){
            String videoUrl = video.getVideoUrl();
            boolean flag = ossComponent.deleteFromOSS(videoUrl);
            if(flag) {
                return Result.success("删除视频成功");
            }else{
                return Result.success("删除视频失败");
            }
        }else{
            return Result.error("删除视频失败");
        }
    }

//    public Result updateVideoIsUse(Integer id, Integer newVideoIsUse, String userId){
//        Video oldVideo = videoMapper.selectByPrimaryKey(id);
//        if(oldVideo.getVideoIsUse().intValue()==newVideoIsUse.intValue()){
//            log.info("视频使用状态相同，无需更新，视频id={}，原使用状态={}，新使用状态={}", id, oldVideo.getVideoIsUse(), newVideoIsUse);
//            return Result.success("视频使用状态无需更新");
//        }
//        Video video = new Video();
//        video.setId(id);
//        video.setVideoIsUse(newVideoIsUse);
//        video.setUpdateTime(new Date(System.currentTimeMillis()));
//        video.setUpdateUserId(userId);
//        int count = videoMapper.updateVideo(video);
//        if(count>0){
//            return Result.success("更新视频使用状态成功");
//        }else{
//            return Result.error("更新视频使用状态失败");
//        }
//    }

    /**
     * 获取所有视频的文件大小总和
     * @return
     */
    public Result getVideoTotalSize(){
        Long totalSize = videoMapper.getVideoTotalSize();
        if(totalSize==null){
            totalSize=0L;
        }
        return Result.success(totalSize);
    }

    /**
     * 设置视频库默认文件夹
     * @param videoFolderParam
     * @return
     */
    public Result updateVideoDefaultFolder(VideoFolderParam videoFolderParam){
        //先清除原有默认文件夹设置
        int count = videoFolderMapper.clearDefaultFolder();
        int count2 = videoFolderMapper.setDefaultFolder(videoFolderParam.getId());
        if(count2>0){
            return Result.success("更新视频库默认文件夹成功");
        }else{
            return Result.success("更新视频库默认文件夹失败");
        }
    }

    public Result useVideo(VideoUseParam videoUseParam){
        Date currentTime = new Date(System.currentTimeMillis());
        videoUseParam.setLastUseTime(currentTime);
        int count = videoMapper.updateVideoUse(videoUseParam);
        if(count>0){
            return Result.success("更新视频使用时间、使用次数成功");
        }else{
            return Result.error("更新视频使用时间、使用次数失败");
        }
    }

    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor=Throwable.class)
    public Result deleteVideo2Recycle(Integer id){
        Video dbVideo = videoMapper.selectByPrimaryKey(id);
        if(dbVideo==null) {
            return Result.error("没有该视频");
        }
        if(dbVideo.getUseCount()>0) {
            return Result.error("该视频被使用，不能删除");
        }
        Video video = new Video();
        video.setId(id);
        video.setDeleteStatus(1);
        Date currentTime = new Date(System.currentTimeMillis());
        video.setUpdateTime(currentTime);
        video.setRecycleTime(currentTime);
        int count = videoMapper.updateVideo(video);
        if(count>0) {
            return Result.success("删除视频到回收站成功");
        }else {
            return Result.error("删除视频到回收站失败");
        }

    }

    /**
     * 批量删除视频（逻辑删除）
     * @param ids
     * @return
     */
    public Result deleteVideo2Recycle(Integer[] ids){
        int failCount = 0;
        int successCount = 0;
        VideoService proxy = (VideoService) AopContext.currentProxy();
        for(Integer id:ids) {
            try {
                Result result = proxy.deleteVideo2Recycle(id);
                if(result.getStatus().intValue()==200) {
                    successCount++;
                }else{
                    failCount++;
                }
            }catch(Exception e) {
                log.error(e.getMessage(), e);
                failCount++;
            }
        }
        if(failCount==0){
            return Result.success("删除"+successCount+"个视频到回收站成功");
        }else if(successCount==0){
            return Result.error("删除"+successCount+"个视频到回收站失败，原因可能是视频被使用", "删除"+successCount+"个视频到回收站失败，原因可能是视频被使用");
        }else{
            return Result.custom(400, failCount+"个视频删除到回收站失败,原因可能是视频被使用"+successCount+"个视频删除到回收站成功", failCount+"个视频删除到回收站失败,"+successCount+"个视频删除到回收站成功");
        }
    }

    public Result moveVideo(Integer[] ids, Integer targetFolderId){
        int failCount = 0;//文件移动失败数量
        int successCount = 0;//文件移动成功数量
        VideoService proxy = (VideoService) AopContext.currentProxy();
        for(Integer id:ids){
            //int count = movePicture(id, targetFolderId);
            int count = 0;
            try{
                count = proxy.moveVideo(id, targetFolderId);
            }catch(Exception e){
                log.error(e.getMessage(), e);
            }
            if(count>0){
                successCount ++;
            }else{
                failCount ++;
            }
        }
        if(failCount==0){
            return Result.success("移动视频成功");
        }else{
            return Result.success(failCount+"个视频移动失败,"+successCount+"个视频移动成功");
        }
    }

    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor=Throwable.class)
    public int moveVideo(Integer id, Integer targetFolderId){
        Video video = new Video();
        video.setId(id);
        video.setFolderId(targetFolderId);
        int count = videoMapper.updateVideo(video);
        return count;
    }

    /**
     * 设置视频全文件夹路径
     * @param videoList
     * @return
     */
    private void buildFullFolderName(List<Video> videoList){
        //设置视频文件的所在目录名称（全路径）
        for(Video video:videoList){
            Integer videoFolderId = video.getFolderId();
            VideoFolder item = getVideoFolder(videoFolderId);
            String itemPath = item.getPath();
            String[] array = itemPath.split("/");
            StringBuffer stringBuffer = new StringBuffer();
            for(int i=1; i<array.length; i++){
                Integer subVideoFolderId = Integer.parseInt(array[i]);
                VideoFolder subItem = getVideoFolder(subVideoFolderId);
                if(i<array.length-1){
                    stringBuffer.append(subItem.getFolderName()).append("/");
                }else{
                    stringBuffer.append(subItem.getFolderName());
                }
            }
            video.setFullFolderName(stringBuffer.toString());
        }
    }

    public PageInfo<Video> queryVideoInRecycle(VideoRecycleQueryParam videoRecycleQueryParam){
        Integer videoType = videoRecycleQueryParam.getVideoType();
        if(videoType!=null){
            String strVideoType = null;
            if(videoType== VideoEnum.VIDEO_MP4.getType()){
                strVideoType=VideoEnum.VIDEO_MP4.getNote();
            }else if(videoType==VideoEnum.VIDEO_MPG.getType()){
                strVideoType=VideoEnum.VIDEO_MPG.getNote();
            }else if(videoType==VideoEnum.VIDEO_AVI.getType()){
                strVideoType=VideoEnum.VIDEO_AVI.getNote();
            }else if(videoType==VideoEnum.VIDEO_MOV.getType()){
                strVideoType=VideoEnum.VIDEO_MOV.getNote();
            }
            videoRecycleQueryParam.setStrVideoType(strVideoType);
        }
        Page<?> page = PageHelper.startPage(videoRecycleQueryParam.getPageNum(), videoRecycleQueryParam.getPageSize());
        List<Video> videoList = videoMapper.queryVideoInRecycle(videoRecycleQueryParam);

        //设置视频文件的所在目录名称（全路径）
        buildFullFolderName(videoList);

        PageInfo<Video> videoPageInfo = new PageInfo<>(videoList);
        return videoPageInfo;
    }

    public Result restoreVideo(Integer[] ids){
        int failCount = 0;
        int successCount = 0;
        VideoService proxy = (VideoService) AopContext.currentProxy();
        for(Integer id:ids){
            try{
                proxy.restoreVideo(id);
                successCount++;
            }catch (Exception e){
                log.error(e.getMessage(), e);
                failCount++;
            }
        }
        if(failCount==0){
            return Result.success("复原音频成功");
        }else{
            return Result.success(failCount+"个音频复原失败,"+successCount+"个音频复原成功");
        }

    }

    @Transactional(rollbackFor = Throwable.class)
    public Result restoreVideo(Integer id){
        Video video = new Video();
        video.setId(id);
        video.setDeleteStatus(0);
        video.setUpdateTime(new Date(System.currentTimeMillis()));
        int count = videoMapper.updateVideo(video);
        if(count>0){
            return Result.success("从回收站恢复视频成功");
        }else{
            return Result.error("从回收站恢复视频失败");
        }

    }

    public Result deleteVideoFromRecycle(Integer[] ids){
        int failCount = 0;
        int successCount = 0;
        VideoService proxy = (VideoService) AopContext.currentProxy();
        for(Integer id:ids){
            try{
                proxy.deleteVideoFromRecycle(id);
                successCount++;
            }catch(Exception e){
                log.error(e.getMessage(), e);
                failCount++;
            }
        }
        if(failCount==0){
            return Result.success("从回收站批量删除"+successCount+"个视频成功");
        }else{
            return Result.success("从回收站删除"+failCount+"个视频失败,"+successCount+"个视频成功");
        }

    }

    @Transactional(rollbackFor = Throwable.class)
    public Result deleteVideoFromRecycle(Integer id){
        //首先查询
        Video video = videoMapper.selectByPrimaryKey(id);
        //其次删除数据库中该视频的记录
        if(video==null){
            return Result.error("没有这个视频id="+id);
        }
        if(video.getDeleteStatus()!=1){
            return Result.error("视频不在回收站中，不能彻底删除,id="+id);
        }
        int count = videoMapper.deleteByPrimaryKey(id);
//        int count = 1;
        if(count>0){
            //最后删除阿里云oss中该视频
            String videoUrl = video.getVideoUrl();;
            boolean b = ossComponent.deleteFromOSS(videoUrl);
            log.info("删除视频库视频{}结果{}", videoUrl, b);
            return Result.success("删除视频成功");
        }else{
            return Result.error("删除视频失败");
        }

    }


    /**
     * 情况视频回收站
     * @return
     */
    public Result clearRecycle(){
        //获取回收站所有的音频id
        List<Integer> ids = videoMapper.listVideoIdInRecycle();
        int failCount = 0;
        int successCount = 0;
        VideoService proxy = (VideoService) AopContext.currentProxy();
        for(Integer id:ids){
            try{
                proxy.deleteVideoFromRecycle(id);
                successCount++;
            }catch(Exception e){
                log.error(e.getMessage(), e);
                failCount++;
            }
        }
        if(failCount==0){
            return Result.success("清空回收站"+successCount+"个视频成功");
        }else{
            return Result.success("清空回收站"+failCount+"个视频失败,"+successCount+"个视频成功");
        }

    }
}
